﻿using System;
using System.Windows.Forms;
using DesignerHelper.Addons;
using SpreadsheetGear;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    internal partial class Margins : UserControl, IScv<IPageSetup>
    {
        public Margins()
        {
            InitializeComponent();
        }

        public Margins(IPageSetup ps) : this()
        {
            TriggerInit(ps);
        }

        private IPageSetup PageSetup { get; set; }

        #region 封装变量

        private double ButtomM
        {
            get { return Convert.ToDouble(numericUpDownButtomM.Value); }
            set { numericUpDownButtomM.Value = Convert.ToDecimal(value); }
        }

        private double TopM
        {
            get { return Convert.ToDouble(numericUpDownTopM.Value); }
            set { numericUpDownTopM.Value = Convert.ToDecimal(value); }
        }

        private double LeftM
        {
            get { return Convert.ToDouble(numericUpDownLeftM.Value); }
            set { numericUpDownLeftM.Value = Convert.ToDecimal(value); }
        }

        private double RightM
        {
            get { return Convert.ToDouble(numericUpDownRightM.Value); }
            set { numericUpDownRightM.Value = Convert.ToDecimal(value); }
        }

        private double HeaderM
        {
            get { return Convert.ToDouble(numericUpDownHeader.Value); }
            set { numericUpDownHeader.Value = Convert.ToDecimal(value); }
        }

        private double FooterM
        {
            get { return Convert.ToDouble(numericUpDownFooter.Value); }
            set { numericUpDownFooter.Value = Convert.ToDecimal(value); }
        }

        private bool HorPrinted
        {
            get { return checkBoxHorPrinted.Checked; }
            set { checkBoxHorPrinted.Checked = value; }
        }

        private bool VecPrinted
        {
            get { return checkBoxVecPrinted.Checked; }
            set { checkBoxVecPrinted.Checked = value; }
        }

        #endregion 封装变量

        public void TriggerInit(IPageSetup ps)
        {
            PageSetup = ps;
            Init();
        }

        public void TriggerReset()
        {
            Init();
        }

        public void TriggerSet()
        {
            Set();
        }

        private void Init()
        {
            IPageSetup ps = PageSetup;

            InitGroupBoxMargin(ps);
            InitGroupBoxCenterPpg(ps);
        }

        private void Set()
        {
            IPageSetup ps = PageSetup;

            SetGroupBoxMargin(ps);
            SetGroupBoxCenterrPgg(ps);
        }

        private void InitGroupBoxMargin(IPageSetup ps)
        {
            ButtomM = TranslatorGet(ps.BottomMargin);
            TopM = TranslatorGet(ps.TopMargin);
            LeftM = TranslatorGet(ps.LeftMargin);
            RightM = TranslatorGet(ps.RightMargin);
            HeaderM = TranslatorGet(ps.HeaderMargin);
            FooterM = TranslatorGet(ps.FooterMargin);
        }

        private void InitGroupBoxCenterPpg(IPageSetup ps)
        {
            HorPrinted = ps.CenterHorizontally;
            VecPrinted = ps.CenterVertically;
        }

        private void SetGroupBoxMargin(IPageSetup ps)
        {
            ps.BottomMargin = TranslatorSet(ButtomM);
            ps.TopMargin = TranslatorSet(TopM);
            ps.LeftMargin = TranslatorSet(LeftM);
            ps.RightMargin = TranslatorSet(RightM);
            ps.HeaderMargin = TranslatorSet(HeaderM);
            ps.FooterMargin = TranslatorSet(FooterM);
        }

        private void SetGroupBoxCenterrPgg(IPageSetup ps)
        {
            ps.CenterVertically = VecPrinted;
            ps.CenterHorizontally = HorPrinted;
        }

        private static double TranslatorGet(double point)
        {
            return point/72; //转化为显示单位英寸Inch，存储单位为Point ps DPI有关?
        }

        private static double TranslatorSet(double inch)
        {
            return inch*72; //转化为存储单位Point 
        }

        #region interface

        void IScv<IPageSetup>.TriggerReset()
        {
            TriggerReset();
        }

        void IScv<IPageSetup>.TriggerSet()
        {
            TriggerSet();
        }

        Control IScv<IPageSetup>.GetControl()
        {
            return this;
        }

        #endregion
    }
}